<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\common\manager;

use app\common\model\Platform\Account;
use app\common\model\Platform\Log;
use app\common\model\Platform\Statistics;
use think\Db;
use think\Exception;

class PlatformManager extends BaseManager
{

    /**
     * 平台账户记录
     * @param $money
     * @param $date
     * @param $directive
     * @param $type
     * @param $extend ['recipient'：接受人id,'relation_id':相关id,'relation_order':相关订单号]
     * @param $trans
     * @return array
     */
    public static function platformLog($money,$date,$directive=Log::DIRECTIVE_OUT,$type=Log::TYPE_TEAM,$extend=[],$trans=true)
    {
        if ($trans)
            Db::startTrans();
        try {
            $result = null;
            $memo = '';
            $directiveMessage = '';
            if($directive == Log::DIRECTIVE_OUT){
                $directiveMessage = Log::DIRECTIVE_OUT_MEMO;
            }else if ($directive == Log::DIRECTIVE_INCOME){
                $directiveMessage = Log::DIRECTIVE_INCOME_MEMO;
            }
            switch ($type){
                case Log::TYPE_TEAM:
                    $memo = sprintf(Log::TYPE_TEAM_MESSAGE,$date,$directiveMessage,$money);
                    break;
                case Log::TYPE_MATCH:
                    $memo = sprintf(Log::TYPE_MATCH_MESSAGE,$date,$directiveMessage,$money);
                    break;
                case Log::TYPE_INVITE:
                    $memo = sprintf(Log::TYPE_INVITE_MESSAGE,$date,$directiveMessage,$money);
                    break;
                case Log::TYPE_ACTIVE:
                    $memo = sprintf(Log::TYPE_ACTIVE_MESSAGE,$date,$directiveMessage,$money);
                    break;
                case Log::TYPE_INVITE_STORE:
                    $memo = sprintf(Log::TYPE_INVITE_STORE_MESSAGE,$date,$directiveMessage,$money);
                    break;
                case Log::TYPE_RECHARGE:
                    $memo = sprintf(Log::TYPE_RECHARGE_MESSAGE,$date,$directiveMessage,$money);
                    break;
                case Log::TYPE_RECHARGE_PROXY_REWARD:
                    $memo = sprintf(Log::TYPE_RECHARGE_PROXY_REWARD_MESSAGE,$date,$directiveMessage,$money);
                    break;
                case Log::TYPE_PROXY_STORE_TEAM_REWARD:
                    $memo = sprintf(Log::TYPE_PROXY_STORE_TEAM_REWARD_MESSAGE,$date,$directiveMessage,$money);
                    break;
                case Log::TYPE_PROXY_CAPTAIN_TEAM_REWARD:
                    $memo = sprintf(Log::TYPE_PROXY_CAPTAIN_TEAM_REWARD_MESSAGE,$date,$directiveMessage,$money);
                    break;
                case Log::TYPE_SYS_ADJUST:
                    $memo = sprintf(Log::TYPE_SYS_ADJUST_MESSAGE,$date,$directiveMessage,$money);
                    break;
                case Log::TYPE_SYS_VIP_PRESENT:
                    $memo = sprintf(Log::TYPE_SYS_VIP_PRESENT_MESSAGE,$date,$directiveMessage,$money);
                    break;
            }
            $result = self::LogStatistics($money,$memo,$date,$directive,$type,$extend['recipient'] ?? 0,$extend['relation_id'] ?? 0,$extend['relation_order'] ?? '',false);
            if ($result['code'] != self::$successCode){
                throw new Exception($result['message']);
            }
            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans)
                Db::rollback();
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData()];
    }

    public static function LogStatistics($money,$memo,$date,$directive,$type,$recipient=0,$relationId=0,$relationOrder='',$trans=true){
        if($trans)
            Db::startTrans();
        try {
            $money = bcadd($money,0,2);
            $accountModel = new Account();
            $account = $accountModel->order('id asc')->find();
            if ($account){
                if ($directive == Log::DIRECTIVE_OUT){
                    $accountModel->where('id',$account['id'])->update([
                            'total'=>Db::raw('total -'.$money),
                            'out_amount'=>Db::raw('out_amount +'.$money),
                            'update_time'=>time(),
                    ]);
                }else if ($directive == Log::DIRECTIVE_INCOME){
                    $accountModel->where('id',$account['id'])->update([
                        'total'=>Db::raw('total +'.$money),
                        'income_amount'=>Db::raw('income_amount +'.$money),
                        'update_time'=>time(),
                    ]);
                }

            }
            $insert = [
                'type'=>$type,
                'directive'=>$directive,
                'recipient'=>$recipient ?? 0,
                'money'=>$money,
                'memo'=>$memo,
                'relation_order'=>$relationOrder,
                'relation_id'=>$relationId,
                'create_time'=>strtotime($date),
                'update_time'=>strtotime($date)
            ];
            if (!Log::create($insert)){
                throw new Exception('平台数据插入失败');
            }

            $year = intval(date('Y',strtotime($date)));
            $month = intval(date('m',strtotime($date)));
            $day = intval(date('d',strtotime($date)));
            if(Statistics::where('type',Statistics::TYPE_YEAR)->where('directive',$directive)->where('year',$year)->find()){
                Statistics::where('type',Statistics::TYPE_YEAR)->where('directive',$directive)->where('year',$year)->update([
                    'money'=>Db::raw('money + '.$money),
                    'update_time'=>time(),
                ]);
            }else{
                Statistics::create([
                    'type'=>Statistics::TYPE_YEAR,
                    'directive'=>$directive,
                    'money'=> $money,
                    'year'=> $year,
                    'create_time'=> time(),
                    'update_time'=> time(),
                ]);
            }

            if($monthInfo = Statistics::where('type',Statistics::TYPE_MONTH)->where('directive',$directive)->where('year',$year)->where('month',$month)->find()){
                Statistics::where('type',Statistics::TYPE_MONTH)->where('directive',$directive)->where('year',$year)->where('month',$month)->update([
                    'money'=>Db::raw('money + '.$money),
                    'update_time'=>time(),
                ]);
            }else{
                Statistics::create([
                    'type'=>Statistics::TYPE_MONTH,
                    'directive'=>$directive,
                    'money'=> $money,
                    'year'=> $year,
                    'month'=> $month,
                    'create_time'=> time(),
                    'update_time'=> time(),
                ]);
            }


            if(Statistics::where('type',Statistics::TYPE_DAY)->where('directive',$directive)->where('year',$year)->where('month',$month)->where('day',$day)->find()){
                Statistics::where('type',Statistics::TYPE_DAY)->where('directive',$directive)->where('year',$year)->where('month',$month)->where('day',$day)->update([
                    'money'=>Db::raw('money + '.$money),
                    'update_time'=>time(),
                ]);
            }else{
                Statistics::create([
                    'type'=>Statistics::TYPE_DAY,
                    'directive'=>$directive,
                    'money'=> $money,
                    'year'=> $year,
                    'month'=> $month,
                    'day'=> $day,
                    'create_time'=> time(),
                    'update_time'=> time(),
                ]);
            }

            if ($trans)
                Db::commit();
            self::setStatusCode(self::$successCode);
            self::setMessage(self::$successMessage);
        }catch (Exception $exception){
            if ($trans)
                Db::rollback();
            self::setStatusCode(self::$failCode);
            self::setMessage($exception->getMessage());
        }
        return ['code'=>self::getStatusCode(),'message'=>self::getMessage(),'data'=>self::getData()];
    }

}